/*
+--------------------------------------------------------------------------
|   Mblog [#RELEASE_VERSION#]
|   ========================================
|   Copyright (c) 2014, 2015 mtons. All Rights Reserved
|   http://www.mtons.com
|
+---------------------------------------------------------------------------
*/
package mblog.base.utils;

import java.io.IOException;
import java.util.Map;

import mtons.modules.exception.MtonsException;

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

/**
 * @author langhsu
 */
public class HttpUtils {

    public static HttpClient getClient() {
        HttpClient client = new HttpClient();
        client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
        return client;
    }

    public static String post(String url, Map<String, String> params) throws IOException {
        PostMethod post = new PostMethod(url);
        for (Map.Entry<String, String> p : params.entrySet()) {
            post.addParameter(p.getKey(), p.getValue());
        }
        return request(post);
    }

    public static String get(String url) throws IOException {
        GetMethod get = new GetMethod(url);
        return request(get);
    }

    public static String request(HttpMethodBase post) throws IOException {
        HttpClient client = getClient();
        int status = client.executeMethod(post);

        if (status != HttpStatus.SC_OK) {
            throw new MtonsException("该地址请求失败");
        }
        return post.getResponseBodyAsString();
    }

}
